package com.diyphonegadgets.freeblog.ui.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.diyphonegadgets.freeblog.R;
import com.diyphonegadgets.freeblog.data.manager.DataManager;
import com.diyphonegadgets.freeblog.ui.adapter.ArticlesListAdapter;
import com.diyphonegadgets.freeblog.utils.Debug;

public class ArticleListFragment extends SherlockFragment {
	protected int mNum;
	protected Handler mHandler;
	protected DataManager mDataManager;
	private ListView articleListView;
	private ArticlesListAdapter articlesListAdapter;
	private static ArticleListFragment f;
	private TextView emptyView;
	private View loadingView;
	private View mainView;

	/**
	 * Create a new instance of ArtistListFragment, providing "num" as an
	 * argument.
	 */
	public static ArticleListFragment newInstance(int num) {
		f = new ArticleListFragment();

		// Supply num input as an argument.
		Bundle args = new Bundle();
		args.putInt("num", num);
		f.setArguments(args);

		return f;
	}

	/**
	 * When creating, retrieve this instance's number from its arguments.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mNum = getArguments() != null ? getArguments().getInt("num") : 1;
		setHasOptionsMenu(true);

		mHandler = new Handler();
		mDataManager = DataManager.getInstance();
		articlesListAdapter = null;
		new InitAsyncTask().execute();

	}

	/**
	 * The Fragment's UI is just a simple text view showing its instance number.
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mainView = inflater
				.inflate(R.layout.articles_list_screen, container, false);
		
		articleListView = (ListView) (mainView.findViewById(R.id.articles_listview));
		articleListView.setDividerHeight(0);
		loadingView = mainView.findViewById(R.id.loading);
		emptyView = (TextView) mainView.findViewById(R.id.empty);
		loadingView.setVisibility(View.GONE);
		articleListView.setEmptyView(loadingView);

		return mainView;
	}

	@Override
	public void onResume() {
		super.onResume();
		new InitAsyncTask().execute();

	}

	public void initArticles() {

		try {
			if (articlesListAdapter == null) {
				articlesListAdapter = new ArticlesListAdapter(getActivity()
						.getApplicationContext(),
						mDataManager.getArticles(getActivity().getApplicationContext()),
						R.layout.article_list_element);
			} else {
				showArticleList();

			}

		}

		catch (Exception e) {

		}
	}

	public void showArticleList() {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				if(articleListView!=null){
				articleListView.setAdapter(articlesListAdapter);
				articleListView.setOnItemClickListener(articlesListAdapter);
				loadingView.setVisibility(View.GONE);
				articleListView.setEmptyView(emptyView);
				}
				else{
					

					
				}

			}
		});
	}

	/**
	 * 
	 * ASYNC TASK
	 * 
	 */

	private class InitAsyncTask extends AsyncTask<Void, Void, Void> {
		@Override
		public Void doInBackground(Void... notUsed) {

			initArticles();
			return null;
		}

		@Override
		public void onPostExecute(Void notUsed) {
			
			
			showArticleList();

		}

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {

		super.onActivityCreated(savedInstanceState);
		//setHasOptionsMenu(true);

	}
	
	

	


	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
	    // TODO Add your menu entries here
	    super.onCreateOptionsMenu(menu, inflater);
	    
	    inflater.inflate(R.menu.share_action_provider, menu);
	}
	

}